/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.atjava.jcrt.core.SystemProperty;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.FileInfo;
import com.atjava.jcrweb.manager.FileManager;
/**
 * 
 * 
 * @author biggie
 * @version $Revision: 1.9 $ $Date: 2008/07/17 02:07:39 $
 */
public class UploadFlexServlet extends FlexBaseServlet {

	private static final Object lockObj = new Object();
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException , JcrWebException ,Exception{
		
		synchronized(lockObj){
			DiskFileItemFactory df = new DiskFileItemFactory();
			df.setSizeThreshold(4096);
			File tempDir = new File(SystemProperty.SYSTEM_REALPATH+File.separator+"WEB-INF"+File.separator+"temp");
			if(!tempDir.isDirectory()){
				tempDir.mkdirs();
			}
			df.setRepository(tempDir);
			ServletFileUpload sf = new ServletFileUpload(df);
			sf.setSizeMax(104857600);//10MB
			FileManager fileMgr = FileManager.getInstance();
			try {
				String path = request.getParameter("path");
				List items = sf.parseRequest(request);
				for(int i=0;i<items.size();i++){
					FileItem item = (FileItem)items.get(i);
					if(item.getName()!=null){
						FileInfo fileinfo = new FileInfo();	
						fileinfo.setId(generatorTimeId());
						fileinfo.setFileName(item.getName());
						fileinfo.setMimeType(item.getContentType());
						fileinfo.setFileSize(item.getSize());
						fileinfo.setExt(item.getName().substring(item.getName().lastIndexOf(".")+1));
						fileinfo.setInputStream(item.getInputStream());
						fileMgr.createFile(path,fileinfo);
					}
				}	
			} catch (FileUploadException e) {
				throw new JcrWebException(e);
			} catch (Exception e) {
				throw new JcrWebException(e);
			}
		}
		return  null;
	}
	private static long lastTimeId =  0;
	private long generatorTimeId(){
		long timeId = System.currentTimeMillis();
		
		if(timeId == lastTimeId){
			try {
				java.lang.Thread.sleep(10);
				generatorTimeId();
			} catch (InterruptedException e) {
				timeId = System.currentTimeMillis();
			}
		}
		lastTimeId = timeId;
		return timeId;
	}
}
